package com.parktech.sun.web.controller.management.contact;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.addressbook.ContactGroup;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.sunhz.ContactGroupService;
import com.parktech.sun.web.controller.management.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 11-1-8
 * Time: 下午2:57
 * To change this template use File | Settings | File Templates.
 */

@Controller
@RequestMapping("/management/contact")
public class ContactGroupController extends BaseController{
    @Autowired
    private ContactGroupService contactGroupService;
    private final String redirectListURL = "redirect:listContactGroups";

    @RequestMapping(value = "/listContactGroups", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView listContactGroups(HttpServletRequest request) {
        Integer currentPageNo = request.getParameter("pageNo") != null ? Integer.parseInt(request.getParameter("pageNo").trim()) : 1;
        ModelAndView modelAndView = new ModelAndView("management/contact/contactGroupList");

        List fieldColumns = new ArrayList();
        List searchValues = new ArrayList();
        if (request.getParameter("searchKeyName") != null) {
            fieldColumns.add("name");
            searchValues.add(request.getParameter("searchKeyName"));
        }
        Page<ContactGroup> returnPage = new Page<ContactGroup>(Constants.PAGINATION_PAGE_SIZE, currentPageNo);
        returnPage.setTotalCount(contactGroupService.getTotalSizeWithSimilarCondition(ContactGroup.class, fieldColumns, searchValues));
        returnPage.setResult(contactGroupService.getPaginationObjectsWithSimilarCondition(ContactGroup.class, currentPageNo, Constants.PAGINATION_PAGE_SIZE, fieldColumns, searchValues, null, null));

        return modelAndView.addObject(returnPage);
    }

    //    @ModelAttribute      @RequestParam
    @RequestMapping(value = "/manageContactGroup",method = RequestMethod.GET)
    public String manageGroup( Model model,HttpServletRequest request) {
        Long contactGroupID = request.getParameter("contactGroupID") != null ? Long.parseLong(request.getParameter("contactGroupID").trim()) : null;
        if (contactGroupID == null)     // request to add the new contact contactGroup object
        {
            model.addAttribute(new ContactGroup());
        } else       // request to update the certain contactGroup object
        {
            ContactGroup contactGroup = contactGroupService.getContactGroupByID(contactGroupID);
            model.addAttribute(contactGroup);
        }
        return "management/contact/contactGroupManage";
    }

    @RequestMapping(value = "/saveManageGroup",method = RequestMethod.POST)
    public String saveManageGroup(@ModelAttribute ContactGroup contactGroup,HttpServletRequest request) {
        Long contactGroupID = contactGroup.getId();
        contactGroupService.saveOrUpdateBean(contactGroup);
        if(contactGroupID == null)
        {
               saveSuccessMessage(request,"创建成功！");
        }
        else
        {
            saveSuccessMessage(request,"修改成功！");
        }

        return "management/contact/contactGroupManage";
    }

    @RequestMapping(value = "/deleteManageGroup",method = RequestMethod.GET)
    public String deleteManageGroup(@RequestParam("contactGroupID") Long contactGroupID) {
        contactGroupService.removeBean(contactGroupID);
        return redirectListURL;
    }

    @RequestMapping(value = "/deleteManageGroups",method = RequestMethod.POST)
    public String deleteManageGroups(HttpServletRequest request) {
        String[] contactGroupIDArray = request.getParameter("selectedGroupIDArray")==null?new String[0]:request.getParameter("selectedGroupIDArray").trim().split(",");
        for (String contactGroupID : contactGroupIDArray) {
            contactGroupService.removeBean(Long.valueOf(contactGroupID.trim()));
        }
        return redirectListURL;
    }

}
